# Files Structure:
# ------------------------------------------------------------
# include/
# obj/
# res/
# src/
# Makefile

DIR_BIN:=.
DIR_SRC:=src
DIR_OBJ:=obj
DIR_INC:=include

EXT_SRC:=c
EXT_INC:=h

INC:=$(wildcard $(DIR_INC)/*.$(EXT_INC))
INC+=$(wildcard $(DIR_SRC)/*.$(EXT_INC))
RES:=

CC:=clang
CFLAGS:=-Wall -std=c11 -DPLATFORM_DESKTOP
CFLAGS+=-I./$(DIR_INC)
# link static lib: -Wl,-Bstatic -l<static>, or /<dir>/lib.a
# link dynamic lib: -Wl,-Bdynamic -l<dynamic>
# system libs required -Wl,-Bdynamic
# e.g. LFLAGS:=-Wl,-Bstatic -l<lib> -Wl,-Bdynamic
LFLAGS:=-lraylib
# console or gui 
WFLAGS:=

ifeq ($(OS),Windows_NT)		# windows
	RES+=$(DIR_OBJ)/resource.res
	WFLAGS+=-Wl,--subsystem,windows
else
	LFLAGS+=-lm
	UNAME=$(shell uname -s)
	ifeq ($(UNAME),Darwin)	# macos
	else					# linux, ...
	endif
endif

.PONY: demo
demo: $(DIR_OBJ)/demo_d.o mkdir
	$(CC) -o $(DIR_BIN)/$@_d $< $(LFLAGS)

.PONY: toold tool
# debug
toold: $(DIR_OBJ)/tool_d.o $(RES) mkdir
	$(CC) -o $(DIR_BIN)/rpaker_d $< $(RES) $(LFLAGS)
# release
tool: $(DIR_OBJ)/tool.o $(RES) mkdir
	$(CC) -s -o $(DIR_BIN)/rpaker $< $(RES) $(LFLAGS) $(WFLAGS)

.PONY: mkdir
mkdir: $(shell mkdir -p $(DIR_OBJ))

$(DIR_OBJ)/%_d.o: $(DIR_SRC)/%.$(EXT_SRC) $(INC)
	$(CC) $(CFLAGS) -D_DEBUG -g -c $< -o $@
$(DIR_OBJ)/%.o: $(DIR_SRC)/%.$(EXT_SRC) $(INC)
	$(CC) $(CFLAGS) -O2 -c $< -o $@

$(DIR_OBJ)/%.res: $(DIR_SRC)/%.rc
	windres -J rc -O coff -i $< -o $@

# clean
.PONY: clean
clean:
	rm -f $(DIR_OBJ)/*.o
	rm -f $(DIR_OBJ)/*.res
	rm -f $(DIR_BIN)/demo_d
	rm -f $(DIR_BIN)/rpaker_d
	rm -f $(DIR_BIN)/rpaker